package com.project.common.operation;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

public class Result<T> implements Serializable {
    private static final long serialVersionUID = -331044934737127448L;
    @ApiModelProperty(
            value = "请求成功/失败",
            example = "true"
    )
    private boolean success;
    private String code;
    @ApiModelProperty(
            value = "业务错误码，请求成功返回：00000000",
            example = "00000000"
    )
    private String errorCode;
    @ApiModelProperty("请求失败时的提示消息，如：系统错误;参数验证失败")
    private String errorMessage;
    @ApiModelProperty("请求成功返回的数据,可以是obj、array、string，或其他类型")
    private T data;

    private Result(Builder<T> builder) {
        this.success = builder.success;
        this.errorCode = builder.errorCode;
        this.code = builder.code;
        this.errorMessage = builder.errorMessage;
        this.data = builder.data;
    }

    public static Result success(Object data) {
        return (new Builder(true, "0")).data(data).build();
    }

    public static Result failure(String errorCode, String errorMessage) {
        return (new Builder(false, "500")).errorCode(errorCode).errorMessage(errorMessage).build();
    }

    public static Result failure(IError error) {
        return (new Builder(false, "0")).error(error).build();
    }

    private Result() {
    }

    public boolean isSuccess() {
        return this.success;
    }

    public String getErrorCode() {
        return this.errorCode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return this.errorMessage;
    }

    public T getData() {
        return this.data;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result) o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.isSuccess() != other.isSuccess()) {
                return false;
            } else {
                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Result;
    }

    public int hashCode() {
        int result = 1;
        result = result * 59 + (this.isSuccess() ? 79 : 97);
        return result;
    }

    public String toString() {
        return "Result(success=" + this.isSuccess() + ", errorCode=" + this.getErrorCode() + ", errorMessage=" + this.getErrorMessage() + ", data=" + this.getData() + ")";
    }

    public static class Builder<T> {
        private boolean success;
        private String code;
        private String errorCode;
        private String errorMessage;
        private T data;

        public Builder(boolean success, String code) {
            this.success = success;
            this.code = code;
            if (success) {
                this.errorCode = "00000000";
            }

        }

        public Builder error(IError error) {
            this.errorCode = error.getCode();
            this.errorMessage = error.getError();
            return this;
        }

        public Builder errorCode(String errorCode) {
            this.errorCode = errorCode;
            return this;
        }

        public Builder errorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
            return this;
        }

        public Builder data(T data) {
            this.data = data;
            return this;
        }

        public Result build() {
            return new Result(this);
        }

        private Builder() {
        }

        public boolean isSuccess() {
            return this.success;
        }

        public String getErrorCode() {
            return this.errorCode;
        }

        public String getErrorMessage() {
            return this.errorMessage;
        }

        public T getData() {
            return this.data;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }

        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }

        public void setData(T data) {
            this.data = data;
        }

        public boolean equals(Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof Builder)) {
                return false;
            } else {
                Builder<?> other = (Builder) o;
                if (!other.canEqual(this)) {
                    return false;
                } else if (this.isSuccess() != other.isSuccess()) {
                    return false;
                } else {
                    return true;
                }
            }
        }

        protected boolean canEqual(Object other) {
            return other instanceof Builder;
        }

        public int hashCode() {
            int result = 1;
            result = result * 59 + (this.isSuccess() ? 79 : 97);
            return result;
        }

        public String toString() {
            return "Result.Builder(success=" + this.isSuccess() + ", errorCode=" + this.getErrorCode() + ", errorMessage=" + this.getErrorMessage() + ", data=" + this.getData() + ")";
        }
    }
}
